package com.hkfg.controller;

import com.hkfg.domain.Blog;
import com.hkfg.domain.User;
import com.hkfg.service.impl.BlogServiceImpl;
import com.hkfg.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller   // bean
@CrossOrigin
//@RestController   // 相当于上面两个
//@RequestMapping("/users")  // 请求路径前缀
public class UserController {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private BlogServiceImpl blogService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public int login(@RequestBody User user){
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        int id = userService.logInCheck(user.getUsername(), user.getPassword());
        if(id> 0){
            return id;   // 返回id
        }else {
            return 0;
        }
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public int register(@RequestBody User user){
        int id = userService.register(user.getUsername(), user.getPassword());
        if(id > 0){
            return id;   // 返回id
        }else {
            return 0;
        }
    }

    /**
     * 初始化
     * @param id
     * @return
     */
    @RequestMapping(value = "/initi", method = RequestMethod.GET)
    @ResponseBody
    public List<Blog> initi(@RequestParam("token")String id){
        return blogService.selectUserBlog(Integer.parseInt(id));
    }




}
